View Javadoc

1   /*
2    * JBCS - A JBuilder Plugin for Checkstyle
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Copyright © 2003
19   * Henri Tremblay
20   */
21  package com.henri.jbcs;
22  
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Properties;
30  import java.util.Set;
31  import java.util.StringTokenizer;
32  
33  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.ArrayList;
37  
38  /***
39   * JBCS preference class. It contains the different paramaters used to configure the plugin.
40   *
41   * @author Henri Tremblay
42   * @version $Revision: 1.6 $ $Date: 2004/09/01 21:42:10 $
43   */
44  public class CSPreference
45  {
46     private static final String HEADER = "JBCS Property File";
47  
48     private static final String TRUE = "true";
49  
50     private static final String FALSE = "false";
51  
52     private static final String CONFIGURATION_FILE = "configurationFile";
53  
54     private static final String PACKAGE_NAME_FILE = "packageNameFile";
55  
56     private static final String PROPERTIES_FILE = "propertiesFile";
57  
58     private static final String RECURSIVE = "recursive";
59  
60     private static final String SHOW_NO_ERROR = "showNoError";
61  
62     private static final String CLEAR_MESSAGE_PANE = "clearMessagePane";
63  
64     private static final String SEVERITIES_SHOWN = "severitiesShown";
65  
66     private static final String EXCLUSION_PATH = "exclusionPath";
67  
68     private Properties m_prop;
69  
70     private Set severitiesShown = new HashSet();
71  
72     public CSPreference() {
73     }
74  
75     public void load(String fileName) throws FileNotFoundException, IOException {
76        m_prop = new Properties();
77        m_prop.load(new FileInputStream(fileName));
78  
79        StringTokenizer strTok = new StringTokenizer(getStringProperty(SEVERITIES_SHOWN, "warning,info,error"), ",");
80        while(strTok.hasMoreTokens()) {
81           String severity = strTok.nextToken();
82           try {
83              SeverityLevel level = SeverityLevel.getInstance(severity);
84              severitiesShown.add(level);
85           }
86           catch(IllegalArgumentException e) {
87              System.out.println("Unknown severity: " + severity);
88           }
89        }
90     }
91  
92     public void store(String fileName) throws FileNotFoundException, IOException {
93        String severityStr = "";
94        for(Iterator it = severitiesShown.iterator(); it.hasNext();) {
95           SeverityLevel level = (SeverityLevel) it.next();
96           severityStr += level.getName() + (it.hasNext() ? "," : "");
97        }
98        m_prop.setProperty(SEVERITIES_SHOWN, severityStr);
99  
100       m_prop.store(new FileOutputStream(fileName), HEADER);
101    }
102 
103    private boolean getBooleanProperty(String key, boolean defaultValue) {
104       String str = m_prop.getProperty(key, defaultValue ? TRUE : FALSE);
105       return str.length() == 0 ? defaultValue : Boolean.valueOf(str).booleanValue();
106    }
107 
108    private String getStringProperty(String key, String defaultValue) {
109       return m_prop.getProperty(key, defaultValue);
110    }
111 
112    private Collection getStringArrayProperty(String key, Collection defaultValue) {
113       String str = m_prop.getProperty(key);
114       if(str == null) {
115          return defaultValue;
116       }
117       Collection result = new ArrayList();
118       StringTokenizer st = new StringTokenizer(str, ",");
119       while(st.hasMoreTokens()) {
120          String path = st.nextToken();
121          result.add(path);
122       }
123       return result;
124    }
125 
126    public boolean getClearMessagePane() {
127       return getBooleanProperty(CLEAR_MESSAGE_PANE, true);
128    }
129 
130    public String getConfigurationFile() {
131       return getStringProperty(CONFIGURATION_FILE, null);
132    }
133 
134    public String getPackageNameFile() {
135       return getStringProperty(PACKAGE_NAME_FILE, null);
136    }
137 
138    public String getPropertiesFile() {
139       return getStringProperty(PROPERTIES_FILE, null);
140    }
141 
142    public boolean getRecursive() {
143       return getBooleanProperty(RECURSIVE, true);
144    }
145 
146    public boolean getShowNoError() {
147       return getBooleanProperty(SHOW_NO_ERROR, false);
148    }
149 
150    public Collection getExclusionPath() {
151       return getStringArrayProperty(EXCLUSION_PATH, Collections.EMPTY_LIST);
152    }
153 
154    /***
155     * If this severity level should be shown in the message pane
156     * @param level Level to check
157     * @return Should it be shown?
158     */
159    public boolean isSeverityShown(SeverityLevel level) {
160       return severitiesShown.contains(level);
161    }
162 
163    public void setSeverityShown(SeverityLevel level, boolean shown) {
164       if(shown) {
165          severitiesShown.add(level);
166       }
167       else {
168          severitiesShown.remove(level);
169       }
170    }
171 
172    public void setClearMessagePane(boolean clearMessagePane) {
173       m_prop.setProperty(CLEAR_MESSAGE_PANE, clearMessagePane ? TRUE : FALSE);
174    }
175 
176    publicng> void setPackageNameFile(String packageNameFile) {
177       m_prop.setProperty(PACKAGE_NAME_FILE, packageNameFile);
178    }
179 
180    public void setPropertiesFile(String propertiesFile) {
181       m_prop.setProperty(PROPERTIES_FILE, propertiesFile);
182    }
183 
184    public void setRecursive(boolean recursive) {
185       m_prop.setProperty(RECURSIVE, recursive ? TRUE : FALSE);
186    }
187 
188    public void setShowNoError(boolean showNoError) {
189       m_prop.setProperty(SHOW_NO_ERROR, showNoError ? TRUE : FALSE);
190    }
191 
192    public void setExlusionPath(Collection exclusionPath) {
193       String value = "";
194       for(Iterator it = exclusionPath.iterator(); it.hasNext();) {
195          String path = (String) it.next();
196          value += path + ",";
197       }
198       if(value.endsWith(",")) {
199          value = value.substring(0, value.length() - 1);
200       }
201       m_prop.setProperty(EXCLUSION_PATH, value);
202    }
203 
204    public void setConfigurationFile(String configurationFile) {
205       m_prop.setProperty(CONFIGURATION_FILE, configurationFile);
206    }
207 }